﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;


namespace TicketTaking
{
    public class ICReader
    {
        // IntPtr dev;
        // //打开设备dll
        // [DllImport("libvbar.dll", EntryPoint = "vbar_open", CallingConvention = CallingConvention.Cdecl)]
        // public static extern IntPtr vbar_open(IntPtr addr, long parm);
        // //背光控制dll
        // [DllImport("libvbar.dll", EntryPoint = "vbar_backlight", CallingConvention = CallingConvention.Cdecl)]
        // public static extern bool vbar_backlight(IntPtr dev, bool toset);
        // //蜂鸣器控制dll
        // [DllImport("libvbar.dll", EntryPoint = "vbar_beep", CallingConvention = CallingConvention.Cdecl)]
        // public static extern bool vbar_beep(IntPtr dev, int times);
        // //判断设备是否已连接dll
        // [DllImport("libvbar.dll", EntryPoint = "vbar_is_connected", CallingConvention = CallingConvention.Cdecl)]
        // public static extern bool vbar_is_connected(IntPtr dev);
        // //添加要支持的码制dll
        // [DllImport("libvbar.dll", EntryPoint = "vbar_add_symbol_type", CallingConvention = CallingConvention.Cdecl)]
        // public static extern bool vbar_add_symbol_type(IntPtr dev, int type);
        // //扫码dll
        // [DllImport("libvbar.dll", EntryPoint = "vbar_scan", CallingConvention = CallingConvention.Cdecl)]
        // public static extern bool vbar_scan(IntPtr dev, ref int type, [MarshalAs(UnmanagedType.LPArray)] byte[] result_buffer, ref int size);
        // //关闭设备dll
        // [DllImport("libvbar.dll", EntryPoint = "vbar_close", CallingConvention = CallingConvention.Cdecl)]
        // public static extern void vbar_close(IntPtr dev);

        public int icdev;

        [DllImport("mwrf32.dll", EntryPoint = "rf_init", SetLastError = true,
               CharSet = CharSet.Auto, ExactSpelling = false,
               CallingConvention = CallingConvention.StdCall)]
        public static extern int rf_init(Int16 port, int baud);

        [DllImport("mwrf32.dll", EntryPoint = "rf_exit", SetLastError = true,
               CharSet = CharSet.Auto, ExactSpelling = false,
               CallingConvention = CallingConvention.StdCall)]
        public static extern Int16 rf_exit(int icdev);

        [DllImport("mwrf32.dll", EntryPoint = "rf_beep", SetLastError = true,
               CharSet = CharSet.Auto, ExactSpelling = false,
               CallingConvention = CallingConvention.StdCall)]
        public static extern Int16 rf_beep(int icdev, int msec);

        [DllImport("mwrf32.dll", EntryPoint = "rf_get_status", SetLastError = true,
              CharSet = CharSet.Auto, ExactSpelling = false,
              CallingConvention = CallingConvention.StdCall)]
        public static extern Int16 rf_get_status(int icdev, byte[] state);

        [DllImport("mwrf32.dll", EntryPoint = "rf_load_key", SetLastError = true,
             CharSet = CharSet.Auto, ExactSpelling = false,
             CallingConvention = CallingConvention.StdCall)]
        public static extern Int16 rf_load_key(int icdev, int mode, int secnr, byte[] keybuff);

        [DllImport("mwrf32.dll", EntryPoint = "rf_load_key_hex", SetLastError = true,
             CharSet = CharSet.Auto, ExactSpelling = false,
             CallingConvention = CallingConvention.StdCall)]
        public static extern Int16 rf_load_key_hex(int icdev, int mode, int secnr, byte[] keybuff);


        [DllImport("mwrf32.dll", EntryPoint = "a_hex", SetLastError = true,
             CharSet = CharSet.Auto, ExactSpelling = false,
             CallingConvention = CallingConvention.StdCall)]
        public static extern Int16 a_hex(byte[] asc, byte[] hex, int len);

        [DllImport("mwrf32.dll", EntryPoint = "hex_a", SetLastError = true,
             CharSet = CharSet.Auto, ExactSpelling = false,
             CallingConvention = CallingConvention.StdCall)]
        public static extern Int16 hex_a(byte[] hex, byte[] asc, int len);

        [DllImport("mwrf32.dll", EntryPoint = "rf_reset", SetLastError = true,
             CharSet = CharSet.Auto, ExactSpelling = false,
             CallingConvention = CallingConvention.StdCall)]
        public static extern Int16 rf_reset(int icdev, int msec);

        [DllImport("mwrf32.dll", EntryPoint = "rf_request", SetLastError = true,
             CharSet = CharSet.Auto, ExactSpelling = false,
             CallingConvention = CallingConvention.StdCall)]
        public static extern Int16 rf_request(int icdev, int mode, out UInt16 tagtype);


        [DllImport("mwrf32.dll", EntryPoint = "rf_anticoll", SetLastError = true,
             CharSet = CharSet.Auto, ExactSpelling = false,
             CallingConvention = CallingConvention.StdCall)]
        public static extern Int16 rf_anticoll(int icdev, int bcnt, out UInt32 snr);

        [DllImport("mwrf32.dll", EntryPoint = "rf_select", SetLastError = true,
             CharSet = CharSet.Auto, ExactSpelling = false,
             CallingConvention = CallingConvention.StdCall)]
        public static extern Int16 rf_select(int icdev, UInt32 snr, out byte size);

        [DllImport("mwrf32.dll", EntryPoint = "rf_card", SetLastError = true,
            CharSet = CharSet.Auto, ExactSpelling = false,
            CallingConvention = CallingConvention.StdCall)]
        public static extern Int16 rf_card(int icdev, int mode, byte[] snr); //这里将第三个参数设置为byte数组，以便直接返回16进制卡号
        //public static extern Int16 rf_card(int icdev, int mode, out UInt32 snr);

        [DllImport("mwrf32.dll", EntryPoint = "rf_authentication", SetLastError = true,
             CharSet = CharSet.Auto, ExactSpelling = false,
             CallingConvention = CallingConvention.StdCall)]
        public static extern Int16 rf_authentication(int icdev, int mode, int secnr);

        [DllImport("mwrf32.dll", EntryPoint = "rf_authentication_2", SetLastError = true,
             CharSet = CharSet.Auto, ExactSpelling = false,
             CallingConvention = CallingConvention.StdCall)]
        public static extern Int16 rf_authentication_2(int icdev, int mode, int keynr, int blocknr);

        [DllImport("mwrf32.dll", EntryPoint = "rf_read", SetLastError = true,
             CharSet = CharSet.Auto, ExactSpelling = false,
             CallingConvention = CallingConvention.StdCall)]
        public static extern Int16 rf_read(int icdev, int blocknr, byte[] databuff);

        [DllImport("mwrf32.dll", EntryPoint = "rf_read_hex", SetLastError = true,
             CharSet = CharSet.Auto, ExactSpelling = false,
             CallingConvention = CallingConvention.StdCall)]
        public static extern Int16 rf_read_hex(int icdev, int blocknr, byte[] databuff);

        [DllImport("mwrf32.dll", EntryPoint = "rf_write_hex", SetLastError = true,
             CharSet = CharSet.Auto, ExactSpelling = false,
             CallingConvention = CallingConvention.StdCall)]
        public static extern Int16 rf_write_hex(int icdev, int blocknr, byte[] databuff);

        [DllImport("mwrf32.dll", EntryPoint = "rf_write", SetLastError = true,
             CharSet = CharSet.Auto, ExactSpelling = false,
             CallingConvention = CallingConvention.StdCall)]
        public static extern Int16 rf_write(int icdev, int blocknr, byte[] databuff);

        [DllImport("mwrf32.dll", EntryPoint = "rf_halt", SetLastError = true,
             CharSet = CharSet.Auto, ExactSpelling = false,
             CallingConvention = CallingConvention.StdCall)]
        public static extern Int16 rf_halt(int icdev);

        [DllImport("mwrf32.dll", EntryPoint = "rf_changeb3", SetLastError = true,
            CharSet = CharSet.Auto, ExactSpelling = false,
            CallingConvention = CallingConvention.StdCall)]
        public static extern Int16 rf_changeb3(int icdev, int sector, byte[] keya, int B0, int B1,
              int B2, int B3, int Bk, byte[] keyb);

        [DllImport("mwrf32.dll", EntryPoint = "rf_pro_rst", SetLastError = true,
            CharSet = CharSet.Auto, ExactSpelling = false,
            CallingConvention = CallingConvention.StdCall)]
        public static extern Int16 rf_pro_rst(int icdev, byte[] _Data);

        [DllImport("mwrf32.dll", EntryPoint = "rf_pro_trn", SetLastError = true,
            CharSet = CharSet.Auto, ExactSpelling = false,
            CallingConvention = CallingConvention.StdCall)]
        public static extern Int16 rf_pro_trn(int icdev, byte[] problock, byte[] recv);

        [DllImport("mwrf32.dll", EntryPoint = "rf_ctl_mode", SetLastError = true,
            CharSet = CharSet.Auto, ExactSpelling = false,
            CallingConvention = CallingConvention.StdCall)]
        public static extern Int16 rf_ctl_mode(int icdev, byte mode);    //受控方式

        [DllImport("mwrf32.dll", EntryPoint = "rf_CtlBackLight", SetLastError = true,
            CharSet = CharSet.Auto, ExactSpelling = false,
            CallingConvention = CallingConvention.StdCall)]
        public static extern Int16 rf_CtlBackLight(int icdev, byte cOpenFlag); //控制背光

        [DllImport("mwrf32.dll", EntryPoint = "rf_LcdClrScrn", SetLastError = true,
            CharSet = CharSet.Auto, ExactSpelling = false,
            CallingConvention = CallingConvention.StdCall)]
        public static extern Int16 rf_LcdClrScrn(int icdev, byte cLine);   //清LCD屏

        [DllImport("mwrf32.dll", EntryPoint = "rf_DispMainMenu", SetLastError = true,
            CharSet = CharSet.Auto, ExactSpelling = false,
            CallingConvention = CallingConvention.StdCall)]
        public static extern Int16 rf_DispMainMenu(int icdev);     //显示欢迎光临

        [DllImport("mwrf32.dll", EntryPoint = "rf_DispLcd", SetLastError = true,
            CharSet = CharSet.Auto, ExactSpelling = false,
            CallingConvention = CallingConvention.StdCall)]
        public static extern Int16 rf_DispLcd(int icdev, byte line, byte type);   //显示系统内置操作

        [DllImport("mwrf32.dll", EntryPoint = "rf_DispInfo", SetLastError = true,
            CharSet = CharSet.Auto, ExactSpelling = false,
            CallingConvention = CallingConvention.StdCall)]
        public static extern Int16 rf_DispInfo(int icdev, byte line, byte offset, byte[] data);  //显示信息，操作前需清屏

        [DllImport("mwrf32.dll", EntryPoint = "rf_disp8", SetLastError = true,
            CharSet = CharSet.Auto, ExactSpelling = false,
            CallingConvention = CallingConvention.StdCall)]
        public static extern Int16 rf_disp8(int icdev, int disp_len, byte[] disp_str);  //在读写器数码管上显示数字

        //连接读卡器
        public bool ConnectIC()
        {
            icdev = rf_init(0, 9600);
            if (icdev > 0)
            {
                return true;
            }
            else
            {
                return true;
            }
        }

        //读卡:返回10进制卡号

        public bool ReadCard(out byte[] result)
        {
            int st;
            byte[] snr = new byte[30];
            st = rf_card(icdev, 0, snr);　　　　//寻卡 
            if (st==0)
            {
                byte[] snr1 = new byte[8];
                hex_a(snr, snr1, 4); 
                result = snr1;
                return true;
            }
            else
            {
                result = null;
                return false;
            }
        }
        //蜂鸣器控制
        public void Beep(int msec)
        {
            int st;
            st = rf_beep(icdev, msec * 10);
            
        }
        //判断设备是否连接
        public bool IsConnected()
        {
            int st;
            byte[] status = new byte[30];
            st = rf_get_status(icdev, status);
            if (st > 0)
            {
                return false;
            }
            else
            {
                return true;
            }
        }
        //关闭设备
        public void CloseDevice()
        {
            rf_exit(icdev);
        }
    }
}
